﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace Pacoca
{
    /// <summary>
    /// Definisão da classe TileMap
    /// </summary>
    class TileMap
    {
        /// <summary>
        /// Esta lista conterá cada imagem de nosso tile
        /// e nos dirá se ele é sólido ou não.
        /// </summary>
        public static List<Tile> tiles = new List<Tile>();

        /// <summary>
        /// Contém todos os retângulos de cada
        /// tile no mapa.
        /// </summary>
        public static List<Rectangle> tileRect;
        //Variável que conterá a informação do mapa.
        public static int[,] map = {                                      
                                        {15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15},
                                        {15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,10},
                                        {15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,10,08},
                                        {15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,10,08,00},
                                        {15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,07,05,06,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,10,08,00,00},
                                        {15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,14,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,10,08,00,00,00},
                                        {15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,07,05,05,05,06,15,15,14,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,10,08,00,00,00,00},
                                        {15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,07,05,05,05,06,15,14,15,15,15,14,15,15,14,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,10,08,00,00,00,00,00},
                                        {15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,07,05,05,05,05,05,06,15,14,15,15,15,14,15,14,15,15,15,14,15,15,14,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,10,08,00,00,00,00,00,00},
                                        {15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,07,05,05,05,06,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,14,15,15,15,15,15,14,15,14,15,15,15,14,15,14,15,15,15,14,15,15,14,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,10,08,00,00,00,00,00,00,00},
                                        {15,15,15,15,15,10,01,01,01,01,01,01,01,11,15,14,15,15,15,14,15,15,15,15,15,15,15,15,15,15,15,15,15,15,03,14,15,15,15,15,15,14,15,14,15,15,15,14,15,14,15,15,15,14,15,15,14,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,10,08,00,00,00,00,00,00,00,00},
                                        {01,01,01,01,01,08,00,00,00,00,00,00,00,09,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,02,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,01,08,00,00,00,00,00,00,00,00,00,},
                                        
                                   };



        //Tamanho do tile.
        private Vector2 tileSize;

        //Posicao do mapa.
        public static int mapPosition = 0;

        private ContentManager Content;

        /// <summary>
        /// Construtor da classe TileMap.
        /// </summary>
        /// <param name="mapSize">Pra definir a dimensão X e Y do tamanho do mapa.</param>
        /// <param name="content">Para poder carregar as mídias do jogo.</param>
        public TileMap(ContentManager Content, Vector2 tileSize)
        {
            this.tileSize = tileSize;
            this.Content = Content;

            for (int x = 1; x < 15; x++)
            {
                String st = "Tiles/Impassable/Bloco"+x;
                tiles.Add(new Tile(Content.Load<Texture2D>(st), TileCollision.Impassable, tileSize));
            }
            tiles.Add(new Tile(Content.Load<Texture2D>("Tiles/Passable/Bloco15"), TileCollision.Passable, tileSize));
            tiles.Add(new Tile(Content.Load<Texture2D>("Tiles/Passable/Bloco16"), TileCollision.Passable, tileSize));
            tiles.Add(new Tile(Content.Load<Texture2D>("Tiles/Passable/Bloco16"), TileCollision.Passable, tileSize));


            
        



        }

        public TileMap() { }

        public void Update(GameTime gameTime, Rectangle ClientBounds)
        {
            Console.WriteLine("" + tiles.Count);
            KeyboardState keyboardState = Keyboard.GetState();

            if ((keyboardState.IsKeyDown(Keys.D) || keyboardState.IsKeyDown(Keys.Right)) &&
                mapPosition < map.GetLength(1) * (int)tileSize.X - ClientBounds.Width)
            {
                Background.velocidadeMapa = Player.velocidadePlayer;
                mapPosition += (int)Player.velocidadePlayer;
                
            }
            else if (mapPosition < map.GetLength(1) * (int)tileSize.X - ClientBounds.Width)
            {
                Background.velocidadeMapa = 1; 
                mapPosition += (int)Background.velocidadeMapa;
            }
            else
            {
                mapPosition = map.GetLength(1) * (int)tileSize.X - ClientBounds.Width;
                Background.paralaxParado = true;
            }


                //Renovar lista
                tileRect = new List<Rectangle>();

                //0 = Dimensão Y.
                for (int y = 0; y < map.GetLength(0); y++)
                {
                    //1 = Dimensão X.
                    for (int x = 0; x < map.GetLength(1); x++)
                    {
                        if (tiles[map[y, x]].collision == TileCollision.Impassable)
                        {
                            tileRect.Add(new Rectangle(x * (int)tiles[map[y, x]].tileSize.X - mapPosition,
                                               y * (int)tiles[map[y, x]].tileSize.Y,
                                               (int)tileSize.X,
                                               (int)tileSize.Y));
                        }
                    }
                }
          }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
            
            //0 = Dimensão Y
            for (int y = 0; y < map.GetLength(0); y++)
            {
                //1 = Dimensão X
                for (int x = 0; x < map.GetLength(1); x++)
                {
                    //ORIGINAL SEM A MARCACAO
                    
                        spriteBatch.Draw(tiles[map[y, x]].texture,
                        new Rectangle((x * (int)tiles[map[y, x]].tileSize.X) - mapPosition,
                                       y * (int)tiles[map[y, x]].tileSize.Y,
                                       (int)tileSize.X,
                                       (int)tileSize.Y),
                        Background.cor);
                    
                     
                    /*
                    //COM MARCACAO
                    Color cor;
                    if ((x == Player.right && y == Player.top) || (x == Player.left && y == Player.top) || (x == Player.right && y == Player.bottom) || (x == Player.left && y == Player.bottom))
                        cor = Color.Red;
                    else
                        cor = Color.White;

                    if (x == Player.centerX)
                    {
                        cor = Color.Blue;
                        Console.WriteLine("" + Player.velPulo);
                    }
                    
                     //if (y == Player.centerY)
                       // cor = Color.Blue;
                        

                    spriteBatch.Draw(tiles[map[y, x]].texture,
                    new Rectangle((x * (int)tiles[map[y, x]].tileSize.X) - mapPosition,
                                   y * (int)tiles[map[y, x]].tileSize.Y,
                                   (int)tileSize.X,
                                   (int)tileSize.Y),
                    cor);
                    */


                }
            }

            spriteBatch.End();
        }
    }
}
